// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package
{
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;

    import gui.fileSetting.FileSettingPanel;
    import gui.itemSetting.LayoutItemSettingPanel;
    import gui.itemView.LayoutItemViewPanel;
    import gui.viewport.LayoutEditViewport;
    import gui.viewport.ViewportOperator;

    import manger.LogicManger;

    import utils.ConfirmBox;
    import utils.MessageBox;

    public class Main extends Sprite
    {
        private var mGUILoader:Loader;
        private var mGUIMc:MovieClip;

        private var mManger:LogicManger;
        private var mLayoutEditViewport:LayoutEditViewport;
        private var mViewportOperator:ViewportOperator;

        private var mItemSettingPanel:LayoutItemSettingPanel;
        private var mItemViewPanel:LayoutItemViewPanel;
        private var mFileSettingPanel:FileSettingPanel;

        private var mGUIDomain:ApplicationDomain;

        private var mConfirmBoxLayer:Sprite;

        public function Main()
        {
            mGUILoader = new Loader();
            mGUILoader.load(new URLRequest("GUI.swf"));
            mGUILoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadGUIComplete);
        }

        private function onLoadGUIComplete(event:Event):void
        {
            mGUIDomain = mGUILoader.contentLoaderInfo.applicationDomain;
            var GUIClass:Class = mGUIDomain.getDefinition("GUI") as Class;
            mGUIMc = new GUIClass();

            this.stage.scaleMode = StageScaleMode.NO_SCALE;
            this.stage.align = StageAlign.TOP_LEFT;
            this.stage.stageWidth = mGUIMc.width;
            this.stage.stageHeight = mGUIMc.height;
            this.stage.color = 0x454545;

            this.addChild(mGUIMc);

            initialize();
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        private function initialize():void
        {
            mManger = new LogicManger();
            mLayoutEditViewport = new LayoutEditViewport();
            mViewportOperator = new ViewportOperator();
            mItemSettingPanel = new LayoutItemSettingPanel();
            mItemViewPanel = new LayoutItemViewPanel();
            mFileSettingPanel = new FileSettingPanel();

            mItemSettingPanel.initialize(mGUIMc["itemSettingPanelMc"], mManger);
            mItemViewPanel.initialize(mGUIMc["itemViewPanelMc"], mManger);
            mViewportOperator.initialize(mGUIMc["operatorMc"], mManger);
            mLayoutEditViewport.initialize(mGUIMc["demoViewportMc"], 640, 960);
            mFileSettingPanel.initialize(mGUIMc["fileSettingPanelMc"], mManger);


            mManger.initialize(mLayoutEditViewport, mViewportOperator,
                               mItemSettingPanel, mItemViewPanel,
                               mFileSettingPanel);

            MessageBox.instance.initialize(mGUIDomain.getDefinition("Message") as Class, mGUIMc);

            mConfirmBoxLayer = new Sprite();
            this.addChild(mConfirmBoxLayer);
            ConfirmBox.instance.initialize(mConfirmBoxLayer, mGUIDomain.getDefinition("ConfirmBox") as Class);

        }
    }
}
